/*  Copyright (C) 2009 Jos van der Til
    
    This file is part of the Fluent Metadata Library.

    The Fluent Metadata Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Fluent Metadata Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with the Fluent Metadata Library.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using FluentMetadata.AttributeBuilders;

namespace FluentMetadata
{
    internal class FluentMetadataTypeDescriptor : CustomTypeDescriptor
    {
        private readonly IDictionary<string, IList<IBaseAttributeBuilder>> _fluentAttributes;
        private Type _objectType;
        private ICustomTypeDescriptor _parent;

        public FluentMetadataTypeDescriptor(IDictionary<string, IList<IBaseAttributeBuilder>> fluentAttributes,
                                            Type objectType, ICustomTypeDescriptor parent)
            : base(parent)
        {
            _fluentAttributes = fluentAttributes;
            _objectType = objectType;
            _parent = parent;
        }

        public override PropertyDescriptorCollection GetProperties()
        {
            PropertyDescriptorCollection baseProperties = base.GetProperties();
            var list = new List<PropertyDescriptor>();
            bool hasCustomAttributes = false;

            foreach (PropertyDescriptor item in baseProperties)
            {
                if (_fluentAttributes.ContainsKey(item.Name))
                {
                    IList<IBaseAttributeBuilder> fluentAttributeCollection = _fluentAttributes[item.Name];
                    if (fluentAttributeCollection.Count > 0)
                    {
                        hasCustomAttributes = true;
                        var attributes = new List<Attribute>();
                        for (int i = 0; i < fluentAttributeCollection.Count; i++)
                        {
                            attributes.AddRange(
                                (fluentAttributeCollection[i] as AbstractAttributeBuilder).GetAttributes());
                        }

                        list.Add(new PropertyDescriptorWrapper(item, attributes.ToArray()));
                    }
                    else
                    {
                        //No Fluent attributes
                        list.Add(item);
                    }
                }
                else
                {
                    list.Add(item);
                }
            }

            if (hasCustomAttributes)
            {
                return new PropertyDescriptorCollection(list.ToArray(), true);
            }
            else
            {
                return baseProperties;
            }
        }
    }

    internal class PropertyDescriptorWrapper : PropertyDescriptor
    {
        private readonly PropertyDescriptor _descriptor;

        public PropertyDescriptorWrapper(PropertyDescriptor descriptor, Attribute[] attributes)
            : base(descriptor, attributes)
        {
            _descriptor = descriptor;
        }

        public override Type ComponentType
        {
            get { return _descriptor.ComponentType; }
        }

        public override bool IsReadOnly
        {
            get { return _descriptor.IsReadOnly; }
        }

        public override Type PropertyType
        {
            get { return _descriptor.PropertyType; }
        }

        public override bool CanResetValue(object component)
        {
            return _descriptor.CanResetValue(component);
        }

        public override object GetValue(object component)
        {
            return _descriptor.GetValue(component);
        }

        public override void ResetValue(object component)
        {
            _descriptor.ResetValue(component);
        }

        public override void SetValue(object component, object value)
        {
            _descriptor.SetValue(component, value);
        }

        public override bool ShouldSerializeValue(object component)
        {
            return _descriptor.ShouldSerializeValue(component);
        }
    }
}